package com.yjd.back.settlement;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.funds.model.FundsPfModel;
import com.yjd.comm.funds.service.IFundsPfService;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by yjd-wap on 2017/11/16.
 */
@Scope("prototype")
@Controller
@RequestMapping("/fundsPfClear/")
public class FundsPfClearControl extends BaseControl {

    /**
     * 平台余额日结算列表
     * @return
     * @throws Exception
     */
    @RequestMapping("list")
    public String list() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        if(paramMap.get("cleardate1")!=null){
            paramMap.put("start_date",paramMap.get("cleardate1").toString().replace("-",""));
        }
        if(paramMap.get("cleardate2")!=null){
            paramMap.put("end_date",paramMap.get("cleardate2").toString().replace("-",""));
        }
        paramMap.put("sort", "clear_date desc");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.fundsPfService.getPageModel(paramMap, DbWREnums.READ),
                ServletUtil.getPageParameterMap(request),
                ServletUtil.getUrl("/fundsPfClear/list"));
        List<FundsPfModel> list = (List<FundsPfModel>) pager.getPagerModel().getDataList();
        for (FundsPfModel fundsPfModel : list) {
            fundsPfModel.getStrMap().put("pay_price", NumberUtil.formatNumber(fundsPfModel.getPay_price(), NumberUtil.FORMAT_4));
            fundsPfModel.getStrMap().put("repayment_a", NumberUtil.formatNumber(fundsPfModel.getRepayment_a(), NumberUtil.FORMAT_4));
            fundsPfModel.getStrMap().put("profit_b", NumberUtil.formatNumber(fundsPfModel.getProfit_b(), NumberUtil.FORMAT_4));
            fundsPfModel.getStrMap().put("repayment_b", NumberUtil.formatNumber(fundsPfModel.getRepayment_b(), NumberUtil.FORMAT_4));
            fundsPfModel.getStrMap().put("merchantr_cash", NumberUtil.formatNumber(fundsPfModel.getMerchantr_cash(), NumberUtil.FORMAT_4));
            fundsPfModel.getStrMap().put("balance", NumberUtil.formatNumber(fundsPfModel.getBalance(), NumberUtil.FORMAT_4));
            fundsPfModel.getStrMap().put("create_time", DateUtil.timestampToDateString(fundsPfModel.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }
        request.setAttribute("pager", pager);
        request.setAttribute("param", paramMap);
        return "fundsPfClear.list";
    }

    /**
     * 生成结算单
     * @throws Exception
     */
    @RequestMapping(value = "generate")
    public void generate() throws Exception {
        String startDate = request.getParameter("cleardate1");
        String endDate = request.getParameter("cleardate2");

        if(StringUtil.isEmpty(startDate)){
            FrameUtil.throwBaseException1("开始日期不能为空");
        }
        if(StringUtil.isEmpty(endDate)){
            FrameUtil.throwBaseException1("结束日期不能为空");
        }
        Long start =  DateUtil.dateStringToTimestamp(startDate,DateUtil.PATTERN_yyyy_MM_dd);
        Long end =  DateUtil.dateStringToTimestamp(endDate,DateUtil.PATTERN_yyyy_MM_dd);
        Double begin_balance = 0d;
        if(start<1507996800L) {
            start = 1507996800L;//设置默认开始时间
        }else {
            Date p_date = DateUtil.addDay(DateUtil.parseDate(startDate.replace("-","") + "", DateUtil.PATTERN_yyyyMMdd), -1);
            Integer queryDate = Integer.parseInt(DateUtil.format(p_date, DateUtil.PATTERN_yyyyMMdd));
            FundsPfModel fundsPfModel = fundsPfService.getModelOne(FrameUtil.newHashMap("clear_date",queryDate),DbWREnums.WRITE);
            if(fundsPfModel==null){
                FrameUtil.throwBaseException1("请先执行前一天余额结算");
            }
            begin_balance = fundsPfModel.getBalance();
        }

        fundsPfService.clearBalance(start,end,begin_balance);
        this.successMsg(ServletUtil.getUrl("/fundsPfClear/list"),"结算单已生成！");
    }


    @Resource
    private IFundsPfService fundsPfService;
}
